package com.sky.service.impl;

import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SeatMealMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;


@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SeatMealMapper seatMealMapper;
    @Override
    public BusinessDataVO businessData() {
        LocalDate now = LocalDate.now();
        LocalDateTime endTime = LocalDateTime.of(now, LocalTime.MAX);
        LocalDateTime beginTime = LocalDateTime.of(now, LocalTime.MIN);
        Integer newUsers = getNewUser(beginTime, endTime);
        Map map = getOrderRate(beginTime, endTime);
        Double orderCompletionRate= (Double)map.get("rate");
        Double turnOver = getTurnOver(beginTime, endTime);
        if(turnOver==null)
        {
            turnOver=0.0;
        }
        if(orderCompletionRate==null)
        {
            orderCompletionRate=0.0;
        }
        Integer validOrderCount = (Integer)map.get("todayValid");
        Double unitPrice=turnOver/validOrderCount;
        //unitPrice取三位小数
        unitPrice=Double.parseDouble(String.format("%.3f",unitPrice));


        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnOver)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        Map map=new HashMap();
        LocalDateTime begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        map.put("begin",begin);
        map.put("status",null);
        Integer allOrders = orderMapper.countOrder(map);
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countOrder(map);
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countOrder(map);
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countOrder(map);
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countOrder(map);
        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .completedOrders(completedOrders)
                .build();
    }

    @Override
    public DishOverViewVO overviewDishes() {
        LocalDateTime begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        Map map = new HashMap<>();
        map.put("begin",begin);
        map.put("status", 0);
        Integer discontinued = dishMapper.countByStatus(map);
        map.put("status", 1);
        Integer sold = dishMapper.countByStatus(map);
        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
      Map map=new HashMap();
      map.put("status",0);
        Integer stopCount = seatMealMapper.countByStatus(map);
        map.put("status",1);
        Integer startCount =seatMealMapper.countByStatus(map);
        return SetmealOverViewVO.builder()
                .discontinued(stopCount)
                .sold(startCount)
                .build();


    }

    @Override
    public BusinessDataVO getBusinessData(LocalDateTime beginTime, LocalDateTime endTime) {


        Integer newUsers = getNewUser(beginTime, endTime);
        Map map = getOrderRate(beginTime, endTime);
        Double orderCompletionRate= (Double)map.get("rate");
        Double turnOver = getTurnOver(beginTime, endTime);
        if(turnOver==null)
        {
            turnOver=0.0;
        }
        if(orderCompletionRate==null)
        {
            orderCompletionRate=0.0;
        }
        Integer validOrderCount = (Integer)map.get("todayValid");
        Double unitPrice=turnOver/validOrderCount;
        //unitPrice取三位小数
        unitPrice=Double.parseDouble(String.format("%.3f",unitPrice));


        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnOver)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();

    }

    private Double getTurnOver(LocalDateTime beginTime, LocalDateTime endTime) {
        Map map = getMap(beginTime, endTime);
        map.put("status",5);
        Double totalMoney = orderMapper.getTotalMoney(map);
        return totalMoney;
    }

    private Map getOrderRate(LocalDateTime beginTime, LocalDateTime endTime) {
        Map map = getMap(beginTime, endTime);
        Integer todayOrders = orderMapper.countOrder(map);
        map.put("status",5);
        Integer todayValid=orderMapper.countOrder(map);
        Double rate=todayValid.doubleValue()/todayOrders;
        Map mapResult=new HashMap();
        mapResult.put("rate",rate);
        mapResult.put("todayValid",todayValid);
        return mapResult;
    }
    private Map getMap(LocalDateTime beginTime,LocalDateTime endTime)
    {
        Map map=new HashMap();
        map.put("begin",beginTime);
        map.put("end",endTime);
        return map;
    }

    private Integer getNewUser(LocalDateTime beginTime, LocalDateTime endTime) {
        Map map = getMap(beginTime, endTime);
        Integer newUsers = orderMapper.countByMap(map);
        return newUsers;
    }
}
